#This script executes event-study regressions to estimate cohort-specific 
#associations between first-generation prenatal exposure to nonattainment 
#and first-generation economic outcomes.

rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(lfe)
library(haven)
library(readr)
library(broom)
library(tidylog)
library(rdrobust)

# Set the working directory
setwd("/projects/opp_env/caa1970/")

# Source external R scripts for custom functions
source("Code/Child_Outcomes/child_outcomes_functions.R")
source("Code/rounding.r")

# Load the first-generation analysis dataset
load("Data/first_gen_analysis_data_jepmicro_acs.rda")

# Event Studies

## Fig B1a -- First-Gen log Real Earnings

eventB1a <- felm(log_real_wages ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor, data=parent_piks_acs %>% 
                 filter(year%in%1969:1975, AGE >33 , AGE < 44, real_wages >0, real_wages < wage_cap, !is.na(lfpr), !is.na(parent_age_min), !is.na(all_fullterm), !is.na(PI_PC)))
summary(eventB1a)

#Save the underlying points
figB1a <- eventB1a %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB1a, file="JPE_Micro/Output/B1_Figures/FigB1a.csv", row.names=F)

## Fig B1b -- First-Gen Positive Real Earnings

eventB1b <- felm(real_wages~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor, data=parent_piks_acs %>% 
                 filter(year%in%1969:1975, AGE >33 , AGE < 44, real_wages > 0, real_wages < wage_cap, !is.na(parent_age_min),!is.na(lfpr), !is.na(all_fullterm)))
summary(eventB1b)

#Save the underlying points
figB1b <- eventB1b %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB1b, file="JPE_Micro/Output/B1_Figure/FigB1b.csv", row.names=F)

## Fig B1c - First-Gen Labor Force Participation

eventB1c <- felm(lfpr~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor, data=parent_piks_acs %>% 
                 filter(year%in%1969:1975, AGE >33 , AGE < 44, real_wages < wage_cap, !is.na(parent_age_min),!is.na(lfpr), !is.na(all_fullterm)))
summary(eventB1c)

#Save the underlying points
figB1c <- eventB1c %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB1c, file="JPE_Micro/Output/B1_Figures/FigB1c.csv", row.names=F)

## Fig B1d - First-Gen Unemployed

eventB1d <- felm(unemployed~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor, data=parent_piks_acs %>% 
                 filter(year%in%1969:1975, AGE >33 , AGE < 44, real_wages < wage_cap, !is.na(parent_age_min),!is.na(lfpr), !is.na(all_fullterm)))
summary(eventB1d)

#Save the underlying points
figB1d <- eventB1d %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB1d, file="JPE_Micro/Output/B1_Figures/FigB1d.csv", row.names=F)

## Fig B1e - First-Gen Any Public Assistance

eventB1e <- felm(any_PA ~inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975 |state_year+county_factor+year_factor+month+
                 survey_year_by_year | 0 |county_factor, data=parent_piks_acs %>% 
                 filter(year%in%1969:1975, AGE >33 , AGE < 44, real_wages < wage_cap, !is.na(parent_age_min),!is.na(lfpr), !is.na(all_fullterm)))
summary(eventB1e)

#Save the underlying points
figB1e <- eventB1e %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB1e, file="JPE_Micro/Output/B1_Figures/FigB1e.csv", row.names=F)